from fastapi import FastAPI
from fastapi.staticfiles import StaticFiles
from fastapi.responses import FileResponse
from fastapi.middleware.cors import CORSMiddleware
from api.endpoints import router as api_router
import os
import scipy.signal
import numpy as np

app = FastAPI()

# 添加CORS中间件
app.add_middleware(
    CORSMiddleware,
    allow_origins=["*"],
    allow_credentials=True,
    allow_methods=["*"],
    allow_headers=["*"],
)

# 确保静态文件目录存在
static_dir = os.path.join("src", "static")
if not os.path.exists(static_dir):
    os.makedirs(static_dir)

# 挂载静态文件目录
app.mount("/static", StaticFiles(directory=static_dir), name="static")

# 添加API路由
app.include_router(api_router, prefix="/api")

@app.get("/")
async def read_root():
    """返回主页"""
    return FileResponse(os.path.join(static_dir, "index.html"))

@app.get("/{path:path}")
async def catch_all(path: str):
    """处理所有其他路径，返回主页"""
    return FileResponse(os.path.join(static_dir, "index.html"))

if __name__ == "__main__":
    import uvicorn
    if not hasattr(scipy.signal, 'hann'):
        scipy.signal.hann = lambda M, sym=True: np.hanning(M)
    uvicorn.run(app, host="127.0.0.1", port=8000)